package com.springboot.show_web.config;

import com.springboot.show_web.component.LoginHandlerInterceptor;
import com.springboot.show_web.component.MyLocalResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by yaosheng on 2019/11/2.
 * <p>
 * 使用WebConfigurerAdapter可以来扩展SpringMVC的功能
 */
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // super.addViewControllers(registry)
        //浏览器发送请求来到页面
        registry.addViewController ("/springboot.show_web").setViewName ("success");
    }

    //所有的WebConfigurerAdapter组建都会一起作用
    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter () {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController ("/").setViewName ("login");
                registry.addViewController ("/login.html").setViewName ("login");
                registry.addViewController ("/main.html").setViewName ("dashboard");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //静态资源：css，js等，SpringBoot已经做好了静态资源的映射
                //SpringBoot已经做好了资源映射
                registry.addInterceptor (new LoginHandlerInterceptor ()).addPathPatterns ("/**")
                        .excludePathPatterns ("index.html", "/", "/user/login");
            }
        };
        return adapter;
    }

    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocalResolver ();
    }
}